package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author chensheng
 * @since 2025-08-08
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param sDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO sDTO) {

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        //select * from shopping_cart where dish_id = ? or setmeal_id = ? and user_id = ?
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .and(qw1 -> qw1.eq(ShoppingCart::getDishId,sDTO.getDishId())
                        .or()
                        .eq(ShoppingCart::getSetmealId,sDTO.getSetmealId()));
        ShoppingCart cart = shoppingCartMapper.selectOne(qw);
        if (cart != null) {
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateById(cart);
        }
        else {
            // 创建新的购物车项
            if (sDTO.getDishId() != null){
                //当前为菜品
                Dish dish = dishMapper.selectById(sDTO.getDishId());
                cart = ShoppingCart.builder()
                        .name(dish.getName())
                        .image(dish.getImage())
                        .userId(BaseContext.getCurrentId())
                        .dishId(dish.getId())
                        .dishFlavor(sDTO.getDishFlavor())
                        .number(1)
                        .amount(dish.getPrice())
                        .createTime(LocalDateTime.now())
                        .build();
            }else {
                //当前为套餐
                SetmealVO setmeal = setmealMapper.getSetmealById(sDTO.getSetmealId());
                cart = ShoppingCart.builder()
                        .name(setmeal.getName())
                        .image(setmeal.getImage())
                        .userId(BaseContext.getCurrentId())
                        .setmealId(setmeal.getId())
                        .number(1)
                        .amount(setmeal.getPrice())
                        .createTime(LocalDateTime.now())
                        .build();
            }

            shoppingCartMapper.insert(cart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .orderByDesc(ShoppingCart::getCreateTime);
        return shoppingCartMapper.selectList(qw);
    }
    /**
     * 清空
     */
    @Override
    public void removeAll() {
        shoppingCartMapper.delete(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,BaseContext.getCurrentId()));
    }
}